﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Globalization;
namespace SAPInvoker
{
   public  class Utility
    {
       private bool DiscoverAPIMetaData(string name)
       {
           //801
           SAP.Connector.SAPConnection con = new SAP.Connector.SAPConnection("ASHOST=10.134.28.99; CLIENT=801;SYSNR=00;USER=sfc;PASSWD=foxestar;LANG=ZF");
            MetaDataReadProxy.ZFMATTRIBUTES atTab = new MetaDataReadProxy.ZFMATTRIBUTES();
            MetaDataReadProxy.ZFMPARAMETERSTable pTabs = new MetaDataReadProxy.ZFMPARAMETERSTable();
            MetaDataReadProxy.ZFMPARAMETERS pTab = new MetaDataReadProxy.ZFMPARAMETERS();
            pTabs.Add(pTab);
            MetaDataReadProxy.ZFMPARAMFIELDSTable fTabs = new MetaDataReadProxy.ZFMPARAMFIELDSTable();
            MetaDataReadProxy.ZFMPARAMFIELDS fTab = new MetaDataReadProxy.ZFMPARAMFIELDS();
            fTabs.Add(fTab);
            MetaDataReadProxy.ZZBIRETURNTable rTabs = new MetaDataReadProxy.ZZBIRETURNTable();
            MetaDataReadProxy.ZZBIRETURN rTab = new MetaDataReadProxy.ZZBIRETURN();
            rTabs.Add(rTab);
           try
           {
               MetaDataReadProxy.MetaDataReadProxy p = new MetaDataReadProxy.MetaDataReadProxy();
               con.Open();
               p.Connection = con;
          
               p.Z_Read_Function_Metadata(name, "EN", out atTab, ref pTabs, ref fTabs, ref rTabs);
           }
           catch (Exception ex)
           {

           }
           finally
           {
               con.Close();
           }
           

           //SAPInvoker invoker = CreateSAPInvoker("Z_READ_FUNCTION_METADATA", this.Connection, true);
           //invoker.InParameters["IN_FUNCTIONNAME"].Value = name;
           //invoker.InParameters["IN_LANGUAGE"].Value = this.connection.GetInternalConnection().Language;
           //try
           //{
           //    invoker.Invoke();
           //}
           //catch (RFCException exception)
           //{
           //    throw new SAPManagedProviderException(exception.Message, SAPManagedProviderErrorCode.RFCFUNCTION_NOTFOUND);
           //}
           DataTable dtObject = pTabs.ToADODataTable();
           DataTable table2 = fTabs.ToADODataTable();
           DataTable table3 = rTabs.ToADODataTable();

           //SAPTable sAPObject = (SAPTable)invoker.Tables["OUT_FMPARAMETERS_TAB"].SAPObject;
           //SAPTable table2 = (SAPTable)invoker.Tables["OUT_FMPARAMFIELDS_TAB"].SAPObject;
           //SAPTable table3 = (SAPTable)invoker.Tables["OUT_RETURN_TAB"].SAPObject;
           if (((table3 == null) || (dtObject == null)) || (table2 == null))
           {
               return false;
           }
           if (((table3.Rows.Count <= 0) || (string.Compare("E", table3.Rows[0][0] as string, true, CultureInfo.InvariantCulture) == 0)) || (string.Compare("A", table3[0][0] as string, true, CultureInfo.InvariantCulture) == 0))
           {
               throw new Exception(table3.Rows[0]["MESSAGE"].ToString() +"RFC not be found!");
           }
           int num = 0;
           for (int i = 0; (i < dtObject.Rows.Count) && (num < table2.Rows.Count); i++)
           {
               DataRow dr = dtObject.Rows[i];
               if (dr["PARAMTYPE"].ToString().CompareTo("X") != 0)
               {
                   Type type;
                   string str = dr["PARAMETER"] as string;
                   string strB = dr["OPTIONAL"] as string;
                   bool isOptional = (strB != null) && (string.Compare("X", strB, true, CultureInfo.InvariantCulture) == 0);
                   string str3 = dr["ISSTRUCTURE"] as string;
                   DataRow rowParameterFields = table2.Rows[num];
                   bool flag2 = ((str3 != null) && (string.Compare("X", str3, true, CultureInfo.InvariantCulture) == 0)) && (rowParameterFields["FIELDNAME"].ToString().Length > 0);
                   int position = int.Parse(dr["PPOSITION"].ToString(), CultureInfo.InvariantCulture);
                   //if (flag2)
                   //{
                   //    type = typeof(string);//new SAPStructureType(str);
                   //    int offset = 0;
                   //    while ((string.Compare(rowParameterFields["PARAMETER"] as string, str, true, CultureInfo.InvariantCulture) == 0) && (string.Compare(dr["PARAMTYPE"].ToString(), rowParameterFields["PARAMTYPE"].ToString(), true, CultureInfo.InvariantCulture) == 0))
                   //    {
                   //        offset = this.AdjustOffsetForAllignment(offset, rowParameterFields);
                   //      //  SAPFieldType fieldType = new SAPFieldType(rowParameterFields["FIELDNAME"] as string, TypeConverter.RFCTYPEString2Enum(rowParameterFields["DATATYPE"] as string), Convert.ToInt32(rowParameterFields["INTLEN"].ToString(), CultureInfo.InvariantCulture), offset, Convert.ToInt32(rowParameterFields["DECIMALS"].ToString(), CultureInfo.InvariantCulture), rowParameterFields["DATATYPE"] as string, rowParameterFields["TABNAME"] as string, rowParameterFields["DDTEXT"] as string, rowParameterFields["OUTPUTLEN"], rowParameterFields["POSITION"], Convert.ToInt32(rowParameterFields["LENG"].ToString(), CultureInfo.InvariantCulture));
                   //     //   (type as SAPStructureType).Add(fieldType);
                   //      //  offset += Convert.ToInt32(rowParameterFields["INTLEN"].ToString(), CultureInfo.InvariantCulture);
                   //        if (++num >= table2.RowCount)
                   //        {
                   //            break;
                   //        }
                   //        rowParameterFields = table2[num];
                   //    }
                   //}
                   //else
                   //{
                   //    num++;
                   //    type = this.ParameterFieldRow2SAPFieldType(str, rowParameterFields);
                   //}
                   if (dr["PARAMTYPE"].ToString().CompareTo("I") == 0)
                   {
                       RFCParameter rfcParam = new RFCParameter(str, type, RFCINOUT.IN, isOptional, this.m_encoding, position);
                       if (string.Compare(structure["DEFAULTVAL"].ToString(), "", true, CultureInfo.InvariantCulture) != 0)
                       {
                           rfcParam.IsDefault = true;
                       }
                       this.AddParameter(rfcParam);
                   }
                   else if (structure["PARAMTYPE"].ToString().CompareTo("E") == 0)
                   {
                       this.AddParameter(new RFCParameter(str, type, RFCINOUT.OUT, isOptional, this.m_encoding, position));
                   }
                   else if (structure["PARAMTYPE"].ToString().CompareTo("T") == 0)
                   {
                       RFCParameter parameter2 = new RFCParameter(str, new SAPTable(type, this.m_encoding), RFCINOUT.TABLE, isOptional, position);
                       if (string.Compare(structure["DEFAULTVAL"].ToString(), "", true, CultureInfo.InvariantCulture) != 0)
                       {
                           parameter2.IsDefault = true;
                       }
                       this.AddParameter(parameter2);
                   }
               }
           }
           return true;
       }
    }
}
